class Solution {
public:
    vector<int> father = vector<int> (201,0);
    void init(){
        for(int i = 0;i < 201;i++){
            father[i] = i;
        }
    }
    int find(int u){
        return u == father[u] ? u : father[u] = find(father[u]);
    }
    void join(int u,int v){
        int x = find(u);
        int y = find(v);
        if(x == y) return;
        father[y] = x;
        father[v] = x;
    }
    bool isSame(int u,int v){
        return find(u) == find(v);
    }
    int findCircleNum(vector<vector<int>>& isConnected) {
        init();
        for(int i = 0;i < isConnected.size();i++){
            for(int j = i + 1;j < isConnected.size();j++){
                if(isConnected[i][j] == 1){
                    join(i+1,j+1);
                }
            }
        }
        int ret = 0;
       
        for(int i = 1;i <= isConnected.size();i++){
            if(father[i] == i){
                ret++;
            }
        }
        
        return ret;
    }
};
